package org.itest.foo;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <p>Title: Foo</p>
 * <p>Date: 2019/10/20 </p>
 * <p>Description: </p>
 *
 * @author xinhe
 */

public class NioTest {

    public static void main(String[] args) throws IOException {

        // test 1
//        RandomAccessFile raf = new RandomAccessFile("/Users/raoweijie/Downloads/test.txt", "rw");

        // test 2
        // 取的是编译后的文件
        getMbb();
        return;
    }

    private static void getMbb() throws IOException {
        File f = new File(NioTest.class.getClassLoader().getResource("test.txt").getFile());
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        FileChannel fc = raf.getChannel();

        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
        while (mbb.hasRemaining()) {
            System.out.println((char) mbb.get());
        }

        // change
        mbb.put(0, (byte) 103);
        raf.close();
    }
}
